package ejemplosAR;

import processing.core.*;
import processing.video.*;
import jp.nyatla.nyar4psg.*;

public class NyARBoardTest extends PApplet {

	private static final long serialVersionUID = 1L;
	private static final String PARA = "/Users/marthagarzon/Desktop/Juan Manuel/Workspace/ModuloSimulacion/data/camera_para.dat";
	private static final String HIRO = "/Users/marthagarzon/Desktop/Juan Manuel/Workspace/ModuloSimulacion/data/patt.hiro";

	private PFont font=createFont("FFScala", 32);
	private Capture cam;
	private NyARBoard nya;
	private int c=0;

	@SuppressWarnings("static-access")
	public void setup() {
		size(640,480,P3D);
		colorMode(RGB, 100);
		println(NyARBoard.VERSION);
		cam=new Capture(this,width,height);
		nya=new NyARBoard(this,width,height,PARA,HIRO,80); //SingleMarker
		nya.setARClipping(100,1000);
		nya.gsThreshold=120;//(0<n<255) default=110
		nya.cfThreshold=0.4;//(0.0<n<1.0) default=0.4
		print(nya.VERSION); 
	}

	public void draw() {
		c++;
		if (cam.available()) {
			background(255);
			cam.read();
			nya.drawBackground(cam);

			if(nya.detect(cam))  
			{
				nya.beginTransform();
				{
					setMatrix(nya.getMarkerMatrix());
					drawBox();
					drawMarkerXYPos();
				}
				nya.endTransform();  
				drawMarkerPatt();
				drawVertex();
			}
		}
	}

	public void drawBox()
	{
		pushMatrix();
		fill(0);
		stroke(255,200,0);
		translate(0,0,20);
		box(40);
		noFill();
		translate(0,0,-20);
		rect(-40,-40,80,80); 
		popMatrix();
	}


	public void drawMarkerPatt()
	{
		PImage p=nya.pickupMarkerImage(40,40,-40,40,-40,-40,40,-40,100,100);  
		image(p,0,0);
	}

	public void drawMarkerXYPos()
	{
		pushMatrix();
		PVector pos=nya.screen2MarkerCoordSystem(mouseX,mouseY);
		translate(pos.x,pos.y,0);
		noFill();
		stroke(0,0,100);
		ellipse(0,0,20-c%20,20-c%20);
		popMatrix();
	}

	public void drawVertex()
	{
		PVector[] i_v=nya.getMarkerVertex2D();
		textFont(font,(float) 10.0);
		stroke(100,0,0);
		for(int i=0;i<4;i++){
			fill(100,0,0);
			ellipse(i_v[i].x,i_v[i].y,6,6);
			fill(0,0,0);
			text("("+i_v[i].x+","+i_v[i].y+")",i_v[i].x,i_v[i].y);
		}
	}

}